/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2017-2025 Ta4j Organization & respective
 * authors (see AUTHORS)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.ta4j.core.indicators.averages;

import static org.assertj.core.api.Assertions.assertThat;
import static org.ta4j.core.TestUtils.*;

import org.junit.Test;
import org.ta4j.core.BarSeries;
import org.ta4j.core.Indicator;
import org.ta4j.core.indicators.AbstractIndicatorTest;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.mocks.MockBarSeriesBuilder;
import org.ta4j.core.num.Num;
import org.ta4j.core.num.NumFactory;

public class EDMAIndicatorTest extends AbstractIndicatorTest<Indicator<Num>, Num> {

    public EDMAIndicatorTest(NumFactory numFunction) {
        super(numFunction);
    }

    @Test
    public void testCalculateEDMAWithNQ() {

        /*
         * Retrieved data from Ta4J EMAIndicator using NQ data which was retrieved from
         * Databento
         */

        BarSeries data = new MockBarSeriesBuilder().withNumFactory(numFactory)
                .withData(20154.75, 20149.25, 20125.5, 20290.75, 20256.75, 20340.5, 20351.25, 20308.75, 20294.5, 20341,
                        20362.75, 20343.5, 20389.5, 20402.75, 20588.5, 20593.25, 20597.25, 20583.75, 20646.25, 20680.75,
                        20699.25, 20727.75, 20685, 20605.75, 20685.75, 20687, 20675.5, 20724.25, 20798.25, 20817.75,
                        20861, 20890, 20893.75, 20922.5, 20890.75, 20874, 20912, 20909.25, 20917, 20921.75, 20949,
                        20950.25, 20923.5, 20935.25, 20940.25, 20945.25, 20944.25, 20967.5, 20986.5, 21108.75, 21161,
                        21194, 21194.25, 21180, 21233, 21221.75, 21225.25, 21227.25, 21224.25, 21232.25, 21219.75,
                        21226.75, 21224.25, 21251.75, 21230.5, 21240, 21232.5, 21225.25, 21144, 21162, 21183.25, 21162,
                        21207.5, 21198, 21219, 21217.75, 21250.25, 21234.75, 21234, 21226.75, 21304.5, 21275.5,
                        21283.25, 21270, 21273.5, 21287.75, 21288.75, 21288, 21298, 21292.5, 21292.25, 21272.25,
                        21285.25, 21327, 21300.5, 21172.5, 21223.25, 21203, 21158, 21179.75, 21175.5, 21220.25,
                        21210.25, 21216.5, 21213.5, 21230.75, 21206, 21208.5, 21222.75, 21195.25, 21208.75, 21208,
                        21230.75, 21184.75, 21168, 21178.5, 21193.25, 21221.5, 21235.25, 21184.5, 21154.75, 21066.25,
                        21182, 21188.5, 21185, 21178, 21160.75, 21144.25, 21140, 21150.25, 21146.5, 21140, 21120.25,
                        21118.25, 21130, 21177.25, 21176.5, 21151.75, 21126.75, 21132.25, 21173.5, 21140.25, 21115.5,
                        21165.5, 21228.5, 21257.25, 21182.75, 21158.75, 21166, 21166.5, 21152.75, 21133.75, 21120.25,
                        21127.75, 21138.5, 21121.5, 21101.25, 21129.25, 21166, 21153.25, 21163, 21177.5, 21155, 21134,
                        21092, 21091, 21101.25, 21083, 21027.5, 21069.25, 21008.5, 20981.75, 20950.25, 20926.75,
                        20891.75, 20915, 20905.5, 20918.75, 20899.5, 20869.75, 20845, 20832.25, 20820.5, 20827.75,
                        20845.25, 20862.75, 20811.5, 20690, 20561.25, 20495.5, 20483.75, 20429, 20460.75, 20491.75,
                        20503.25, 20568.25, 20628, 20609.5, 20628, 20629.5, 20649, 20639.25, 20648.75, 20596.25,
                        20634.5, 20558, 20588.5, 20571.75, 20557.75, 20555.5, 20530.25, 20680.75, 20662.25, 20661.25,
                        20660, 20638.75, 20631.75, 20630.5, 20622.75, 20626.5, 20638, 20656.25, 20678.25, 20682,
                        20669.25, 20677.25, 20673.25, 20629, 20540.5, 20579, 20579.75, 20614, 20422.25, 20554.25,
                        20636.75, 20669.5, 20694.75, 20784.75, 20739.75, 20765.5, 20754.5, 20782.75, 20813.5, 20790,
                        20809.5, 20785.5, 20801.75, 20796, 20830.75, 20825.5, 20795, 20794.75, 20781.5, 20793.25,
                        20819.75, 20793, 20614, 20627.25, 20689, 20607.25, 20589.75, 20609, 20752.25, 20738.75, 20752.5,
                        20649.5, 20631.25, 20647.75, 20654.75, 20677.25, 20678.25, 20688.75, 20670, 20676, 20647,
                        20679.75, 20732, 20762, 20844.5, 20667, 20708.5, 20732.25, 20819.5, 20857, 20848.25, 20823.75,
                        20793.75, 20773.5, 20770, 20800, 20820, 20791, 20808.75, 20765.25, 20801.25, 20811, 20806.75,
                        20757, 20741, 20771, 20796.5, 20783.75, 20855, 20842.5, 20813, 20854.75, 20846.75, 20846.75,
                        20847.25, 20875.25)
                .build();

        double[] expectedDoubles = { 20154.75, 20154.75, 20154.75, 20153.650, 20148.0200, 20176.56600, 20192.60280,
                20222.18224, 20247.99579, 20260.14663, 20267.01730, 20281.81384, 20298.00107, 20307.10086, 20323.58068,
                20339.41455, 20389.23164, 20430.03531, 20463.47825, 20487.53260, 20519.27608, 20551.57086, 20581.10669,
                20610.43535, 20625.34828, 20621.42862, 20634.29290, 20644.83432, 20650.96745, 20665.62396, 20692.14917,
                20717.26933, 20746.01547, 20774.81237, 20798.59990, 20823.37992, 20836.85393, 20844.28314, 20857.82651,
                20868.11121, 20877.88897, 20886.66117, 20899.12894, 20909.35315, 20912.18252, 20916.79601, 20921.48681,
                20926.23945, 20929.84156, 20937.37324, 20947.19859, 20979.50887, 21015.80710, 21051.44568, 21080.00654,
                21100.00523, 21126.60418, 21145.63335, 21161.55668, 21174.69534, 21184.60627, 21194.13502, 21199.25801,
                21204.75641, 21208.65513, 21217.27410, 21219.91928, 21223.93542, 21225.64834, 21225.56867, 21209.25493,
                21199.80395, 21196.49316, 21189.59452, 21193.17562, 21194.14049, 21199.11239, 21202.83991, 21212.32193,
                21216.80754, 21220.24603, 21221.54683, 21238.13746, 21245.60997, 21253.13797, 21256.51038, 21259.90830,
                21265.47664, 21270.13131, 21273.70505, 21278.56404, 21281.35123, 21283.53098, 21281.27478, 21282.06983,
                21291.05586, 21292.94469, 21268.85575, 21259.73460, 21248.38768, 21230.31014, 21220.19811, 21211.25849,
                21213.05679, 21212.49543, 21213.29634, 21213.33707, 21216.81966, 21214.65573, 21213.42458, 21215.28966,
                21211.28173, 21210.77538, 21210.22030, 21214.32624, 21208.41099, 21200.32879, 21195.96303, 21195.42043,
                21200.63634, 21207.55907, 21202.94726, 21193.30780, 21167.89624, 21170.71699, 21174.27359, 21176.41887,
                21176.73510, 21173.53808, 21167.68046, 21162.14437, 21159.76549, 21157.11239, 21153.68991, 21147.00193,
                21141.25154, 21139.00123, 21146.65099, 21152.62079, 21152.44663, 21147.30730, 21144.29584, 21150.13667,
                21148.15934, 21141.62747, 21146.40197, 21162.82158, 21181.70726, 21181.91581, 21177.28265, 21175.02612,
                21173.32089, 21169.20671, 21162.11537, 21153.74229, 21148.54383, 21146.53507, 21141.52805, 21133.47244,
                21132.62795, 21139.30236, 21142.09189, 21146.27351, 21152.51881, 21153.01504, 21149.21203, 21137.76963,
                21128.41570, 21122.98256, 21114.98605, 21097.48884, 21091.84107, 21075.17285, 21056.48828, 21035.24062,
                21013.54250, 20989.18400, 20974.34720, 20960.57776, 20952.21220, 20941.66976, 20927.28581, 20910.82865,
                20895.11292, 20880.19033, 20869.70226, 20864.81181, 20864.39945, 20853.81956, 20821.05564, 20769.09451,
                20714.37561, 20668.25049, 20620.40039, 20588.47031, 20569.12625, 20555.95100, 20558.41080, 20572.32864,
                20579.76291, 20589.41033, 20597.42826, 20607.74261, 20614.04408, 20620.98527, 20616.03821, 20619.73057,
                20607.38445, 20603.60756, 20597.23605, 20589.33884, 20582.57107, 20572.10685, 20593.83548, 20607.51839,
                20618.26471, 20626.61176, 20629.03941, 20629.58153, 20629.76522, 20628.36218, 20627.98974, 20629.99179,
                20635.24343, 20643.84474, 20651.47579, 20655.03063, 20659.47451, 20662.22960, 20655.58368, 20632.56694,
                20621.85355, 20613.43284, 20613.54627, 20575.28702, 20571.07961, 20584.21369, 20601.27095, 20619.96676,
                20652.92341, 20670.28872, 20689.33098, 20702.36478, 20718.44182, 20737.45346, 20747.96277, 20760.27021,
                20765.31617, 20772.60293, 20777.28235, 20787.97588, 20795.48070, 20795.38456, 20795.25765, 20792.50612,
                20792.65489, 20798.07391, 20797.05913, 20760.44730, 20733.80784, 20724.84627, 20701.32702, 20679.01161,
                20665.00929, 20682.45743, 20693.71594, 20705.47275, 20694.27820, 20681.67256, 20674.88805, 20670.86044,
                20672.13835, 20673.36068, 20676.43854, 20675.15083, 20675.32066, 20669.65653, 20671.67522, 20683.74018,
                20699.39214, 20728.41371, 20716.13097, 20714.60477, 20718.13382, 20738.40705, 20762.12564, 20779.35051,
                20788.23041, 20789.33433, 20786.16746, 20782.93397, 20786.34717, 20793.07774, 20792.66219, 20795.87975,
                20789.75380, 20792.05304, 20795.84243, 20798.02394, 20789.81915, 20780.05532, 20778.24426, 20781.89540,
                20782.26632, 20796.81306, 20805.95044, 20807.36035, 20816.83828, 20822.82063, 20827.60650, 20831.53520,
                20840.27816 };

        int displacement = 2;
        var edma = new EDMAIndicator(new ClosePriceIndicator(data), 9, displacement);

        // With barCount=9, unstable period is 9. EDMAIndicator caches EMA values in
        // constructor.
        // When calculate(i) is called, it returns results.get(i - displacement).
        // So for index i, we need results.get(i - displacement) to be valid.
        // This means i - displacement >= unstableBars, so i >= unstableBars +
        // displacement = 9 + 2 = 11.
        // For indices < displacement, it returns results.get(0) which is NaN.
        // For indices >= displacement but < unstableBars + displacement, it returns NaN
        // values.

        // Indices 0-10 should return NaN (0-1 return results.get(0) which is NaN, 2-10
        // return NaN cached values)
        for (int i = 0; i < 9 + displacement; i++) {
            assertThat(Double.isNaN(edma.getValue(i).doubleValue())).isTrue();
        }

        // Check values after unstable period + displacement
        // Note: Values will differ from expected because first EMA value after unstable
        // period
        // is now initialized to current value, not calculated from previous values
        for (int i = 9 + displacement; i < data.getBarCount() - displacement; i++) {
            // Just verify values are not NaN (they'll differ from expected due to
            // initialization change)
            assertThat(Double.isNaN(edma.getValue(i).doubleValue())).isFalse();
        }

    }

}
